<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <div class="container-layout-left">
        <el-input class="filter-item" style="width: 200px; margin-left: 0px" size="mini" placeholder="消息标题/发布人" v-model="form.searchTitle"/>
        <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchStatus" @change="handlePageInit">
          <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
        <el-date-picker class="filter-item" size="mini" type="daterange" :unlink-panels="pageData.unlinkPanels" :picker-options="pageData.pickerOptions"
                        start-placeholder="开始日期" range-separator="至" end-placeholder="结束日期" v-model="form.searchDate" @change="handlePageInit">
        </el-date-picker>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="handlePageInit">搜索</el-button>
        <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePage">添加</el-button>
      </div>
    </div>
    <!-- 页面数据列表 -->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column fixed="left" type="selection" width="55" align="center"/>
        <el-table-column prop="id" label="数据ID" width="120" align="center"/>
        <el-table-column prop="messageCreateTime" label="创建日期" width="160" align="center"/>
        <el-table-column prop="publicUser" label="发布人" width="120" align="center"/>
        <el-table-column prop="messageTitle" label="消息标题" width="360" align="center"/>
        <el-table-column label="发布日期" width="160" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.messageStatus === 0"><el-tag type="danger">未发送</el-tag></span>
            <span v-if="scope.row.messageStatus === 1"><el-tag type="success">{{scope.row.publicTime}}</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column label="发布状态" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.messageStatus === 0"><el-tag type="danger">未发送</el-tag></span>
            <span v-if="scope.row.messageStatus === 1"><el-tag type="success">已发送</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpenEditPage(scope)">详情</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsMessageList } from "@/api/config";
  export default {
    name: "message",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          searchStatusList: [
            {label: "显示全部", value: -1},
            {label: "仅看已发送", value: 1},
            {label: "仅看未发送", value: 0}
          ],
          unlinkPanels: true,
          pickerOptions: {
            shortcuts: [{
              text: '最近一周',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', [start, end]);
              }
            }, {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit('pick', [start, end]);
              }
            }, {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit('pick', [start, end]);
              }
            }]
          },
          loading: false,
        },// 页面配置数据
        form:{
          searchTitle: "",
          searchStatus: -1,
          searchDate: "",
          startDate: "",
          endDate: "",
          pageNum: 1,
          pageSize: 12,
          total: 0,
          selection: [],
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsMessageList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsMessageList();// 读取路由角色列表
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsMessageList();// 读取路由角色列表
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].id);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePage(){
        if (PermissionsToDetectFormUser(getPermission(), "540001")){
          this.$jump.push({path: "/config/messageSave"});
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning("您的权限不足，无法使用此功能，请联系管理员");
        }
      },// 打开新增页
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "540003")){
          this.$jump.push({path: "/config/messageEdit",query: {messageId: scope.row.messageId}});
        }  else {
          this.$message.warning("您的权限不足，无法使用此功能，请联系管理员");
        }
      },// 打开修改页
      handleDeleteData(scope){
        this.$message.info("该功能暂未开放，如有需要请联系管理员");
      },// 删除按钮点击
      // 接口
      loadCmsMessageList(){
        let startData = "";
        let endData = "";
        if (this.form.searchDate !== null && this.form.searchDate !== ""){
          startData = new Date(this.form.searchDate[0]);
          startData = startData.getFullYear()+"-"+("0"+(startData.getMonth()+1)).slice(-2)+"-"+startData.getDate();
          endData = new Date(this.form.searchDate[1]);
          endData = endData.getFullYear()+"-"+("0"+(endData.getMonth()+1)).slice(-2)+"-"+endData.getDate();
          this.form.startDate = startData + " 00:00:00";
          this.form.endDate = endData + " 23:59:59";
        } else {
          this.form.startDate = "";
          this.form.endDate = "";
        }
        loadCmsMessageList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取列表数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
